package db.tables;


//import java.sql.Statement;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import control.ConnectionManage;


import db.parking.beans.ParkingLot;


public class ParkingLotAdmin {
	
	private static Connection conn = ConnectionManage.getInstance().getConnection();

	/*public static void displayAllRows() throws SQLException {

		String sql = "SELECT *  FROM Comlaints";
		try (
				Statement stmt = conn.createStatement();
				ResultSet rs = stmt.executeQuery(sql);
				){

			System.out.println("Complaints Table:");
			while (rs.next()) {
				StringBuffer bf = new StringBuffer();
				bf.append(rs.getInt("id") + ": ");
				bf.append(rs.getString("username") +", ");
				bf.append(rs.getString("age"));
				System.out.println(bf.toString());
			}
		}
	}
*/
	public static ParkingLot getRow(int lotId) throws SQLException {

		String sql = "SELECT * FROM ParkingLot WHERE lotID = ?";
		ResultSet rs = null;

		try (
				PreparedStatement stmt = conn.prepareStatement(sql);
				){
			stmt.setInt(1, lotId);
			rs = stmt.executeQuery();

			if (rs.next()) {
				ParkingLot bean = new ParkingLot();
				bean.setLotID(lotId);
				bean.setCapacity(rs.getInt("capacity"));
				bean.setTotalCars(rs.getInt("totalCars"));
				bean.setX(rs.getInt("x"));
				bean.setY(rs.getInt("y"));
				bean.setZ(rs.getInt("z"));

				return bean;
			} else {
				return null;
			}

		} catch (SQLException e) {
			System.err.println(e);
			return null;
		} finally {
			if (rs != null) {
				rs.close();
			}
		}

	}

	
	//update complaint status
	public static boolean update(int lotID,int newNumOfCars) throws Exception {

		String sql =
				"UPDATE ParkingLot SET " +
				"totalCars = ? " +
				"WHERE lotID = ?";
		try (
				PreparedStatement stmt = conn.prepareStatement(sql);
				){
			
			stmt.setInt(1,newNumOfCars);
			stmt.setInt(2,lotID);
			
			int affected = stmt.executeUpdate();
			if(affected == 1){
				return true;
			}else{
				return false;
			}
			
		}
		catch(SQLException e) {
			System.err.println(e);
			return false;
		}

	}
	
	
}
